package models

import (
	"time"

	log "github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
)

type IpccSipRouterNumber struct {
	Id         int
	Number     string
	RouterAddr string
	Port       int
	Active     bool
	CreateTime time.Time
}

func GetRouterAddrByNumber(number string) (addr string, port int, err error) {

	result := &IpccSipRouterNumber{
		Number: number,
	}

	o := orm.NewOrm()
	o.Using("default")

	err = o.QueryTable(new(IpccSipRouterNumber)).Filter("Number", number).OrderBy("-CreateTime").One(result)
	if err != nil {
		log.Error("query table err:%v", err)
		return
	}
	addr = result.RouterAddr
	port = result.Port

	return
}

type IpccSipRouterBrand struct {
	Id         int
	Brand      string
	RouterAddr string
	Port       int
	Active     bool
	CreateTime time.Time
}

func GetRouterAddrByBrand(brand string) (addr string, port int, err error) {

	result := &IpccSipRouterBrand{
		Brand: brand,
	}

	o := orm.NewOrm()
	o.Using("default")

	err = o.QueryTable(new(IpccSipRouterBrand)).Filter("Brand", brand).OrderBy("-CreateTime").One(result)
	if err != nil {
		log.Error("query table err:%v", err)
		return
	}

	addr = result.RouterAddr
	port = result.Port

	return
}

func RegIpccSipRouter() {
	orm.RegisterModel(new(IpccSipRouterNumber), new(IpccSipRouterBrand))
}
